package com.lxh.lxhcrm.model.service;

import com.lxh.lxhcrm.model.dao.afterSale.*;
import com.lxh.lxhcrm.model.mappers.afterSale.*;
import com.lxh.lxhcrm.model.pojos.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 开发团队 ：lxh
 * 开发人员 ：李兴洋
 * 开发时间 ：2020年3月25日14:13:27
 * 类的名称 ：WorkordService
 * 类的描述 ：维修服务
 */

@Service
public class WorkordService {

    @Autowired
    WorkordMapper workordMapper;

    @Autowired
    WorkordDao workordDao;

    @Autowired
    CostRecordDao costRecordDao;

    @Autowired
    SerDetailDao serDetailDao;

    @Autowired
    WeiStateDao weiStateDao;


//维修工单
    //查询所有维修工单
    public List<Workord> findAllWorkord(String search,String kcName){
        return workordMapper.findAllWorkord(search,search,kcName);
    }

    //查询所有维修工单
    public List<Map> findAllWorkordByTname(){
        List<WeiState> weiStates = workordMapper.allWeiState();

        List<List<Workord>> allWorkord = new ArrayList<List<Workord>>();
        List<Map<String,Object>> numList = new ArrayList<Map<String,Object>>();

        for (WeiState weiState : weiStates) {
//            System.out.println(weiState+"->");
            allWorkord.add(workordMapper.findAllWorkord("","", weiState.getWeiName()));
            numList.add(workordMapper.wNum( weiState.getWeiName()));
        }

        List<Map> resList = new ArrayList<Map>();

        for (int i = 0;i<weiStates.size();i++) {
            Map<String,Object> tmap = new HashMap<String,Object>();
            tmap.put("tableName",weiStates.get(i).getWeiName());
            tmap.put("workData",allWorkord.get(i));
            tmap.put("wCount",numList.get(i));
            resList.add(tmap);
        }

        //System.out.println("hahaha");

        return resList;
    }

    //添加维修信息
    public Workord addWorkord (Workord workord){
        Date date = new Date();
        workord.setTakeTime(date);
        Workord resultWorkord = workordDao.save(workord);
        return resultWorkord;
    }

    //删除工单
    public void deleteWorkord(Integer wid){workordDao.deleteById(wid);}

    //添加维修工单详情表
    public void addSerDetail(SerDetail serDetail){
        serDetailDao.save(serDetail);
    }

    //添加维修费用表
    public void addCostRecord(CostRecord costRecord){
        costRecordDao.save(costRecord);
    }



    //根据工单id查工单详情
    public SerDetail findSdByWid(Integer wid){
        return workordMapper.findWorkordDetailByWorkordId(wid);
    }

    //根据工单id查工单费用
    public CostRecord findCRByWid(Integer wid){
        return workordMapper.findCostRecordByWorkordId(wid);
    }

    // 根据客户id查找客户购买所有商品
    public List<Product> findProByCusId(Integer customerId){
        return workordMapper.findProductByCusId(customerId);
    }

    // 合同里的客户
    public List<Map<String,Object>> findCusForContract(){
        return workordMapper.cusListForContract();
    }

    //工单审批
    public void workordCheck(Integer wId){
        workordMapper.updateCheck(wId);
    }

    /** 修改维修状态 **/
    public void updateWeiState(String weiState,Integer workordId){
        workordMapper.updateWeiState(weiState,workordId);
    }

    /** 所有维修状态**/
    public List<WeiState> allWeiState(){
        return workordMapper.allWeiState();
    }

    //添加维修状态
    public void addWeiState(WeiState weiState){
        weiStateDao.save(weiState);
    }

    //删除维修状态
    public void deleteWeiState(Integer weiStateId){
        weiStateDao.deleteById(weiStateId);
    }

}
